<?php

/**
 * This is the model class for table "{{state}}".
 *
 * The followings are the available columns in table '{{state}}':
 * @property integer $id
 * @property integer $countryID
 * @property string $name
 * @property string $created
 * @property string $modified
 */
class State extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return State the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{state}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('state_name,countryID', 'required'),
			array('state_name', 'length', 'max'=>300),
			array('state_name', 'unique', 'message'=>'This state already exists.'),
			array('created, modified', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('stateID, countryID, state_name, created, modified', 'safe', 'on'=>'search'),
			array('modified','default',
           	  'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'update'),
        	array('created,modified','default',
              'value'=>new CDbExpression('NOW()'),
              'setOnEmpty'=>false,'on'=>'insert'),
			array('stateID, state_name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'stateID' => 'ID',
			'countryID'=>'COUNTRY',
			'state_name' => 'STATE NAME',
			'created' => 'CREATED',
			'modified' => 'MODIFIED',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('stateID',$this->stateID);
		$criteria->compare('countryID',$this->countryID);
		$criteria->compare('state_name',$this->state_name,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	
	
	public static function getList($type,$arrParams)
	{
		switch ($type)
		{
			case 'STATE-LIST':
				$AllStateList = array();
				$arrStateList = State::model()->findAll();
				foreach ($arrStateList as $state)
				{
					$AllStateList[$state->stateID] = $state->state_name;
				}
			return $AllStateList;
			break;
			case 'STATE-LIST-LIKE':
				$AllStateList = array();
				$arrStateList = State::model()->findAll('state_name LIKE :qterm',array(':qterm'=>$arrParams['qterm']));
				foreach ($arrStateList as $state)
				{
					$AllStateList[$state->stateID] = $state->state_name;
				}
			return $AllStateList;
			break;
			case 'BY-COUNTRY-ID':
			return State::model()->findAll('countryID=:countryID',array(':countryID'=>$arrParams['countryID']));
			break;
		}
	}
	public function getModel($type,$arrParams)
	{
		switch ($type)
		{
			case 'BY-ID':
			return State::model()->findByPk((int)$arrParams['id']);
			break;
			
			case 'STATE-LIKE':
			return State::model()->find(array(
					'condition' => "state_name LIKE :match",      
					'params'    => array(':match' => "%".$arrParams['state_name']."%")
				));
			break;	
			
		}
	}
	public function getStateNameByID($id)
	{
		$model = State::model()->findByPk((int)$id);
		return $model->state_name;
	}
	
	/* Gives list of states */
	public function getStates()
	{
		$arrstates = array();
		$models = State::model()->findAll();
		foreach($models as $model){
			$arrstates[$model->stateID]=$model->state_name;
		}
		 return $arrstates;
	}
}